﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class admin_module_categories : System.Web.UI.UserControl
{
    categoryBus catbus = new categoryBus();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getCategories();
        }
    }

    public void getCategories()
    {
        DataTable dt = catbus.getCategoriesByLanguageID(1);
        DataTable list_categories = new DataTable();
        list_categories.Columns.Add("id");
        list_categories.Columns.Add("name");
        list_categories.Columns.Add("href");
        DataRow dr;
        foreach (DataRow r in dt.Rows)
        {
            dr = list_categories.NewRow();
            dr["id"]=r["category_id"];
            dr["name"] = this.getNameByID(Convert.ToInt32(r["category_id"]),1).ToString();
            dr["href"] = "~//admin//default.aspx?action=form_category&path=" + Convert.ToInt32(r["category_id"]);
            list_categories.Rows.Add(dr);
        }
       
        list.DataSource = list_categories;
        list.DataBind();
        if (list_categories.Rows.Count == 0)
        {
            text_empty.Visible = true;
        }
    }
    public static void ClickEdit(object value)
    {
        
    }

    public string getNameByID(int category_id,int language_id){
        string output="";

        ArrayList arr = catbus.getCategoryByCategoryID(category_id, language_id);
        if(arr!=null){
            
            string parent = "";
            if (Convert.ToInt32(arr[1])!=0)
            {
                parent = this.getNameByID(Convert.ToInt32(arr[1]), language_id);
            
            }
            if (parent != "")
            {
                output += parent + " >> ";
            }
            output += arr[0].ToString();
        }
        return output;

    }

    protected void list_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int id = Convert.ToInt32(list.DataKeys[e.Item.ItemIndex]);
        catbus.DeleteCategoryByID(id);
        getCategories();    
        
    }
    
}